package com.kuangstudy.controller.reg;

import com.kuangstudy.entity.User;
import com.kuangstudy.service.UserService;
import com.kuangstudy.vo.R;
import io.swagger.annotations.ApiOperation;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.reflect.generics.visitor.Reifier;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: xuke
 * @time: 2021/5/22 20:05
 */
@RestController
public class RegController {


    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("reguser")
    @ApiOperation("用户注册")
    public R regUser(User user) {
        // 1: 先把用户注册到DB中wie
        //userService.saveOrUpdate(user);
        // 2： 然后查询最新的用户信息放入到redis的hash重
        //User user1 = userService.getById(user.getId());
        Map<String, Object> map = R.beanToMap(user);
        // 3: 准备用存入的key，将用户信息存入到redis的hash中
        String key = "reg:user:" + user.getId();
        redisTemplate.opsForHash().putAll(key, map);
        // 4: 设置key的失效时间一个月
        redisTemplate.expire(key, 30, TimeUnit.DAYS);
        return R.ok();
    }
}
